<template>
  <el-row :gutter="20">
    <el-col :span="8">
      <div class="grid-content bg-purple">
        <wareTree @handleNodeClick="handleNodeClick"></wareTree>
      </div>
    </el-col>
    <el-col :span="16">
      <div class="grid-content bg-purple">
        <div class="app-container">
          <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true"
            v-show="showSearch" label-width="68px">
            <el-form-item label="检索" prop="search">
              <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                v-hasPermi="['carbon:ware:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                v-hasPermi="['carbon:ware:remove']">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="wareList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column type="index" width="50" label="编号" />
            <el-table-column label="仓库" align="center" prop="wareNameDto" />
            <el-table-column label="库区" align="center" prop="wareAreaDto" />
            <el-table-column label="库位" align="center" prop="warePositionDto" />
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
                  @change="handleStatusChange(scope.row)">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="createBy" width="120">
              <template slot-scope="scope">
                <el-tag effect="dark" type="danger" v-if="scope.row.createBy != null">
                  {{ scope.row.createBy }}
                </el-tag>
                <el-tag effect="dark" type="info" v-else>
                  暂无创建人
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="创建日期" align="center" prop="createTime" width="100">
              <template slot-scope="scope">
                <el-tag effect="dark" type="danger" v-if="scope.row.createTime != null">
                  <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </el-tag>
                <el-tag effect="dark" type="info" v-else>
                  暂无日期
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="修改人" align="center" prop="updateBy" width="120">
              <template slot-scope="scope">
                <el-tag effect="dark" type="danger" v-if="scope.row.updateBy != null">
                  {{ scope.row.updateBy }}
                </el-tag>
                <el-tag effect="dark" type="info" v-else>
                  暂无修改人
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="修改日期" align="center" prop="updateTime" width="100">
              <template slot-scope="scope">
                <el-tag effect="dark" type="danger" v-if="scope.row.updateTime != null">
                  <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
                </el-tag>
                <el-tag effect="dark" type="info" v-else>
                  暂无日期
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['carbon:ware:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['carbon:ware:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

          <!-- 添加或修改仓库对话框 -->
          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item :label="(form.level == '1' ? '仓库' : (form.level == '2' ? '库区' : '库位'))" prop="wareName">
                <el-input v-model="form.wareName"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                  <el-radio :label="'1'">隐藏</el-radio>
                  <el-radio :label="'0'" checked>显示</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="form.sort" @change="handleChange" :min="0" :max="10" label="请选择排序">
                </el-input-number>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </div>
      </div>
    </el-col>
  </el-row>
</template>


<script>
import { changeWareStatus, getCascaderWare, listWare, getWare, delWare, addWare, updateWare } from "@/api/wms/ware";
import wareTree from '@/views/wms/ware/wareTree.vue';
export default {
  name: "Ware",
  components: {
    wareTree
  },
  data () {
    return {
      // 仓库列表
      options: [],
      props: {
        checkStrictly: true,
        value: 'id',
        label: 'wareName',
        children: 'children'

      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      wareList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wareName: [
          { required: true, message: "wareName不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    this.getList();
    this.getOptions();
  },
  methods: {
    handleChange (value) {
      this.form.sort = value
    },
    // 子节点 自定义 事件 传递数据
    handleNodeClick (data) {
      console.log(data);

      // 显示分类对应的 
      if (data.level != 3) {
        this.queryParams.wareId = data.id
        this.queryParams.level = data.level
        this.getList()
      }

    },
    // 修改仓库状态
    handleStatusChange (row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + row.wareName + '"吗？').then(function () {
        return changeWareStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList()
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 格式化数据，递归将空的children置为undefined
    formatData (data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i].children = undefined;
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.formatData(data[i].children)
        }
      }
      return data;
    },
    // 获取级联仓库数据
    getOptions () {
      getCascaderWare().then(res => {
        this.options = this.formatData(res.data)
      })
    },
    /** 查询仓库列表 */
    getList () {
      this.loading = true;
      listWare(this.queryParams).then(response => {
        this.wareList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        wareName: null,
        parentId: null,
        delFlag: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getWare(id).then(response => {
        this.form = response.data;
        this.open = true;
        if (this.form.level == '1') {
          this.title = "修改仓库";
        }
        else if (this.form.level == '2') {
          this.title = "修改库区";
        }
        else {
          this.title = "修改库位";
        }

      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWare(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWare(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除仓库编号为"' + ids + '"的数据项？').then(function () {
        return delWare(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

  }
};
</script>
